package com.itany.corejava.day12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestChange implements ActionListener {

    private JFrame jf;
    private JTextField jtf1;
    private JTextField jtf2;
    private JButton jb;

    public TestChange() {
        jf=new JFrame();
        jtf1=new JTextField(10);
        jtf2=new JTextField(10);
        jb=new JButton("交换");
    }

    private void init(){
        jf.setLayout(new FlowLayout());
        jf.add(jtf1);
        jf.add(jtf2);
        jf.add(jb);
    }

    private void addEventHandler(){
        jb.addActionListener(this);
    }

    public void show(){
        init();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestChange().show();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("TestChange.actionPerformed");
        String s1=jtf1.getText();
        String s2=jtf2.getText();

        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);

        jtf1.setText(s2);
        jtf2.setText(s1);
    }
}
